//
//  ImageEdit.m
//  nurseApp
//
//  Created by cyptt on 2022/8/31.
//

#import "ImageEdit.h"

@implementation ImageEdit

#pragma mark --压缩base64
/*
 压缩base64
 */
+(NSString *)compressionBase64:(NSString *)imageBase64 toByte:(NSUInteger)maxLength{
    NSData * decodeImgData = [[NSData alloc]initWithBase64EncodedString:imageBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
   NSLog(@"大小kb----%lf",decodeImgData.length/1024.0);

    NSString * ImageStr  = @"";
    if (decodeImgData.length < maxLength) {
        ImageStr = imageBase64;
         
    }else{
        UIImage * img = [UIImage imageWithData:decodeImgData];
        NSData * lastData = [ImageEdit compressImageQuality:img toByte:maxLength];
        ImageStr = [lastData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    }
    return  ImageStr;
}

#pragma mark --压缩图片尺寸
/*
 压缩尺寸到某个程度
 */
+ (UIImage *)compressImageSize:(UIImage *)image toByte:(NSUInteger)maxLength {
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        // Use image to draw (drawInRect:), image is larger but more compression time
        // Use result image to draw, image is smaller but less compression time
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, 1);
    }
    return resultImage;
}

/*
 压缩尺寸到某个程度
 */
+ (NSData *)compressImageSizeData:(UIImage *)image toByte:(NSUInteger)maxLength {
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        // Use image to draw (drawInRect:), image is larger but more compression time
        // Use result image to draw, image is smaller but less compression time
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, 1);
    }
    return data;
}
/*
 压缩尺寸宽度多宽，长度等比压缩
 */
+ (UIImage *)compressImageSize:(UIImage *)image toWithSize:(CGFloat)withLength {
    
    if (image.size.width < withLength) {
        
        return image;
    }
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
  
   
    CGFloat ratio = withLength / image.size.width;
    
    CGSize size = CGSizeMake((NSUInteger)(withLength),
                             (NSUInteger)(resultImage.size.height * ratio)); // Use NSUInteger to prevent white blank
    UIGraphicsBeginImageContext(size);
    // Use image to draw (drawInRect:), image is larger but more compression time
    // Use result image to draw, image is smaller but less compression time
    [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    data = UIImageJPEGRepresentation(resultImage, 1);
    NSLog(@"大小kb%lf",data.length/1024.0);
    return resultImage;
}

/*
 压缩尺寸高度，宽度等比压缩
 */
+ (UIImage *)compressImageSize:(UIImage *)image toHightSize:(CGFloat)hightLength {
    
    if (image.size.height < hightLength) {
        
        return image;
    }
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
  
   
    CGFloat ratio = hightLength / image.size.width;
    
    CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * ratio),
                             (NSUInteger)(hightLength)); // Use NSUInteger to prevent white blank
    UIGraphicsBeginImageContext(size);
    // Use image to draw (drawInRect:), image is larger but more compression time
    // Use result image to draw, image is smaller but less compression time
    [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    data = UIImageJPEGRepresentation(resultImage, 1);
    NSLog(@"大小kb%lf",data.length/1024.0);
    return resultImage;
}

#pragma mark --只压缩质量
//压缩图片得到图片
+(UIImage *)compressionImg:(UIImage *)img toByte:(NSInteger)maxLength{

    NSData * lastData = [ImageEdit compressImageQuality:img toByte:maxLength];
    
    NSLog(@"压缩后大小kb----%lf",lastData.length/1024.0);
    UIImage * resultImage = [[UIImage alloc]initWithData:lastData];
    return  resultImage;
}

//压缩图片得到data
+(NSData *)compressionImgData:(UIImage *)img toByte:(NSInteger)maxLength;{

    NSLog(@"%f==%f",img.size.width,img.size.height);
    NSData * lastData = [ImageEdit compressImageQuality:img toByte:maxLength];
    return  lastData;
}
/**
 压缩图片到指定大小(byte) 不压缩尺寸

 @param image 原始图片
 @param maxLength 最大长度
 @return 压缩后图片
 */
+ (NSData *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength {
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    NSLog(@"大小kb----%lf",data.length/1024.0);
    if (data.length < maxLength){
        
        return data;
    }
    CGFloat max = 1;
    CGFloat min = 0;
   //个人感觉6次已经足够了
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        //这里是为了防止压缩后过小我们让图片在0.9-1.0之间
    
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    return data;
}
#pragma mark --压缩图片质量和尺寸
/*
 压缩质量和尺寸 先压缩质量后压缩尺寸到指定大小
 */
+(NSData *)compressQualityAndSizeImgData:(UIImage *)img toByte:(NSInteger)maxLength{
    NSData * lastData = [ImageEdit compressImageQuality:img toByte:maxLength];
    if (lastData.length > maxLength) {
        //再压缩尺寸
        UIImage * qualityImage = [[UIImage alloc]initWithData:lastData];
        
        lastData  =  [ImageEdit compressImageSizeData:qualityImage toByte:maxLength];
    }
    return  lastData;
}

/*
 压缩图片尺寸和质量
 */
+(UIImage *)compressQualityAndSizeImg:(UIImage *)img toByte:(NSInteger)maxLength{
    NSData * lastData = [ImageEdit compressImageQuality:img toByte:maxLength];
    if (lastData.length > maxLength) {
        //再压缩尺寸
        UIImage * qualityImage = [[UIImage alloc]initWithData:lastData];
        
        lastData  =  [ImageEdit compressImageSizeData:qualityImage toByte:maxLength];
    }
    UIImage * lastImage = [UIImage imageWithData:lastData];
    return  lastImage;
}

@end
